package org.hotpotmaterial.code.service.impl;

import java.util.List;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaElement;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;

/**
 * 注释插件
 * 
 * @author xmd
 *
 */
public class CommentPlugin extends PluginAdapter {
	private String interfaceName;
	private boolean deleteMethod = true;

	private FullyQualifiedJavaType interfaceType;

	private FullyQualifiedJavaType E;
	private FullyQualifiedJavaType M;
	private FullyQualifiedJavaType MLIST;
	private FullyQualifiedJavaType ID;

	@Override
	public boolean validate(List<String> warnings) {
		interfaceName = this.properties.getProperty("interfaceName");
		String deleteMethod = this.properties.getProperty("deleteMethod");
		if ("FALSE".equals(deleteMethod.toUpperCase())) {
			this.deleteMethod = Boolean.getBoolean(deleteMethod);
		} else {
			this.deleteMethod = true;
		}

		E = new FullyQualifiedJavaType("E");
		M = new FullyQualifiedJavaType("M");
		MLIST = new FullyQualifiedJavaType("List<M>");
		ID = new FullyQualifiedJavaType("ID");

		String interfacePack = context.getJavaClientGeneratorConfiguration().getTargetPackage();
		interfaceType = new FullyQualifiedJavaType(interfacePack + "." + interfaceName);
		return true;
	}

	/**
	 * 生成的Mapper接口
	 *
	 * @param interfaze
	 * @param topLevelClass
	 * @param introspectedTable
	 * @return
	 */
	@Override
	public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass,
			IntrospectedTable introspectedTable) {
		if (deleteMethod) {
			// 清空导入的包

			// 获取实体类
			FullyQualifiedJavaType entityType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

			// 注释的添加
			addMapperComment(interfaze, introspectedTable);

			FullyQualifiedJavaType exampleType = new FullyQualifiedJavaType(introspectedTable.getExampleType());
			interfaze.addImportedType(exampleType);

			if (introspectedTable.getPrimaryKeyColumns() == null || introspectedTable.getPrimaryKeyColumns().isEmpty()) {
				interfaze.addImportedType(new FullyQualifiedJavaType("该表没设置主键"));
			} else {
				interfaze.addImportedType(introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType());
			}

			// import实体类
			interfaze.addImportedType(entityType);
			return true;
		} else {
			return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
		}
	}

	/**
	 * 生成example
	 */
	@Override
	public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		// 注释的添加
		addExampleClassComment(topLevelClass, introspectedTable);
		return true;
	}

	private void addMapperComment(JavaElement javaElement, IntrospectedTable introspectedTable) {
		javaElement.addJavaDocLine("/**");
		javaElement.addJavaDocLine(" * ");
		javaElement.addJavaDocLine(" * " + introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime() + "的dao接口 ");
		javaElement.addJavaDocLine(" */");
	}

	public void addExampleClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * ");
		topLevelClass.addJavaDocLine(" * " + introspectedTable.getFullyQualifiedTable() + "的example类");
		topLevelClass.addJavaDocLine(" */");

	}

	/**
	 * 添加方法
	 */
	protected Method countByExample(IntrospectedTable introspectedTable) {
		Method method = new Method();
		method.setName("countByExample");
		method.setReturnType(FullyQualifiedJavaType.getIntInstance());
		method.addParameter(new Parameter(E, "example"));
		method.setVisibility(JavaVisibility.PUBLIC);
		context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
		return method;
	}
}